/*
 *    Copyright 2022 jacky4631 shen ji email:121659050@qq.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.dataoke.shoppingguide.app773026.activity.home.fragment;


import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.dataoke.shoppingguide.app773026.R;
import com.dataoke.shoppingguide.app773026.activity.home.fragment.category.CategoryFragment;
import com.dataoke.shoppingguide.app773026.activity.home.fragment.food.FoodFragment;
import com.dataoke.shoppingguide.app773026.activity.home.fragment.me.UserInfoFragment;
import com.dataoke.shoppingguide.app773026.activity.home.fragment.top.TopFragment;
import com.dataoke.shoppingguide.app773026.base.fragments.BaseFragment;
import com.dataoke.shoppingguide.app773026.widget.NavigationButton;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 首页含导航
 * @author: shen ji
 * @date: 2022/3/15
 * @email: 121659050@qq.com
 * @wechat: jacky4631
 */
public class NavFragment extends BaseFragment implements View.OnClickListener{
    @BindView(R.id.nav_item_home)
    NavigationButton mNavHome;
    @BindView(R.id.nav_item_top)
    NavigationButton mNavTop;
    @BindView(R.id.nav_item_eat)
    NavigationButton mNavEat;
    @BindView(R.id.nav_item_dress)
    NavigationButton mNavDress;
    @BindView(R.id.nav_item_me)
    NavigationButton mNavMe;
    private Context mContext;
    private int mContainerId;
    private FragmentManager mFragmentManager;
    private NavigationButton mCurrentNavButton;
    public NavFragment() {
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_nav;
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void initWidget(View root) {
        super.initWidget(root);

        ShapeDrawable lineDrawable = new ShapeDrawable(new RectShape());
        lineDrawable.getPaint().setColor(getResources().getColor(R.color.white));
        LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{
                new ColorDrawable(getResources().getColor(R.color.white)),
                lineDrawable
        });
        root.setBackgroundDrawable(layerDrawable);

        mNavHome.init(R.drawable.tab_icon_home,
                R.string.main_tab_name_news,
                HomeFragment.class);
        mNavTop.init(R.drawable.tab_icon_top,
                R.string.main_tab_name_top,
                TopFragment.class);
        mNavEat.init(R.drawable.tab_icon_love,
                R.string.main_tab_name_tweet,
                FoodFragment.class);

        mNavDress.init(R.drawable.tab_icon_yi,
                R.string.main_tab_name_category,
                CategoryFragment.class);

        mNavMe.init(R.drawable.tab_icon_me,
                R.string.main_tab_name_my,
                UserInfoFragment.class);
    }

    @OnClick({R.id.nav_item_home, R.id.nav_item_top, R.id.nav_item_eat,
            R.id.nav_item_dress, R.id.nav_item_me})
    @Override
    public void onClick(View v) {
        if (v instanceof NavigationButton) {
            NavigationButton nav = (NavigationButton) v;
            doSelect(nav);
        }
    }

    public void setup(Context context, FragmentManager fragmentManager, int contentId) {
        mContext = context;
        mFragmentManager = fragmentManager;
        mContainerId = contentId;

        // do clear
        clearOldFragment();
        // do select first
        //预加载榜单页面
        doSelect(mNavTop);
        doSelect(mNavHome);
    }

    @SuppressWarnings("RestrictedApi")
    private void clearOldFragment() {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        List<Fragment> fragments = mFragmentManager.getFragments();
        if (transaction == null || fragments == null || fragments.size() == 0)
            return;
        boolean doCommit = false;
        for (Fragment fragment : fragments) {
            if (fragment != this && fragment != null) {
                transaction.remove(fragment);
                doCommit = true;
            }
        }
        if (doCommit)
            transaction.commitNow();
    }

    private void doSelect(NavigationButton newNavButton) {

        NavigationButton oldNavButton = null;
        if (mCurrentNavButton != null) {
            oldNavButton = mCurrentNavButton;
            oldNavButton.setSelected(false);
        }
        newNavButton.setSelected(true);
        doTabChanged(oldNavButton, newNavButton);
        mCurrentNavButton = newNavButton;
    }

    private void doTabChanged(NavigationButton oldNavButton, NavigationButton newNavButton) {
        FragmentTransaction ft = mFragmentManager.beginTransaction();
        if (oldNavButton != null) {
            if (oldNavButton.getFragment() != null) {
                ft.detach(oldNavButton.getFragment());
            }
        }
        if (newNavButton != null) {
            if (newNavButton.getFragment() == null) {
                Fragment fragment = Fragment.instantiate(mContext,
                        newNavButton.getClx().getName(), null);
                ft.add(mContainerId, fragment, newNavButton.getTag());
                newNavButton.setFragment(fragment);
            } else {
                ft.attach(newNavButton.getFragment());
            }
        }
        ft.commit();
    }
}
